//
//	  main.c,  takes an int from the user via console and prints it as english words
//
//
//	  Copyright (c) 2011 <John Lawniczak>
//	  jlawniczak340license@gmail.com
//
//    This file is part of CS202_Assignment1.
//
//    CS202_Assignment1 is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, version 3 of the License.
//
//    CS202_Assignment1 is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with CS202_Assignment1.  If not, see <http://www.gnu.org/licenses/>.
//

#include <stdio.h>

#define BOOL unsigned char
#define TRUE 1
#define FALSE 0

/**************************************************
function Name: parselargeNumber
Input to the method: int[] that is the formatted array with the 0th index containing millions,
		1st thousands and 2nd hundreds, int RawNumber the number to split up into sections
Output(Return value): none (we fill the int[] to be sent to the user)
Brief description of the task: takes the int[] and converts it to a single integer
Author: John Lawniczak
**************************************************/
void parselargeNumber(int sections[], int RawNumber) {
	int hundreds, thousands, millions;
	hundreds = RawNumber % 1000;
	RawNumber /= 1000;
	sections[2] = hundreds;
	thousands = RawNumber % 1000;
	RawNumber /= 1000;
	sections[1] = thousands;
	millions = RawNumber;
	RawNumber /= 1000;
	sections[0] = millions;
}


/**************************************************
function Name: printTeen
Input to the method: int represents the ones place of the teen number to print
Output(Return value): (none)
Brief description of the task: prints the teen number corresponding to the unit passed to it
Author: John Lawniczak
**************************************************/
void printTeen(int units){
	switch (units) {
	case 1:
		printf(" eleven ");
		break;
	case 2:
		printf(" twelve ");
		break;
	case 3:
		printf(" thirteen ");
		break;
	case 4:
		printf(" fourteen ");
		break;
	case 5:
		printf(" fifteen ");
		break;
	case 6:
		printf(" sixteen ");
		break;
	case 7:
		printf(" seventeen ");
		break;
	case 8:
		printf(" eighteen ");
		break;
	case 9:
		printf(" nineteen ");
		break;
	case 0:
		printf(" ten ");
		break;
	}
}


/**************************************************
function Name: printDigit
Input to the method: int represents the digit to print
Output(Return value): (none)
Brief description of the task: prints the digit as a straight number in english
Author: John Lawniczak
**************************************************/
void printDigit (int digit){
	switch (digit) {
	case 1:
		printf(" one ");
		break;
	case 2:
		printf(" two ");
		break;
	case 3:
		printf(" three ");
		break;
	case 4:
		printf(" four ");
		break;
	case 5:
		printf(" five ");
		break;
	case 6:
		printf(" six ");
		break;
	case 7:
		printf(" seven ");
		break;
	case 8:
		printf(" eight ");
		break;
	case 9:
		printf(" nine ");
		break;
	}

}

/**************************************************
function Name: printTens
Input to the method: int represents the single tens digit
Output(Return value): wether we need to print the tens and untis as the funky english teen words
Brief description of the task: takes the input and prints it as if it was the tens place of a number
Author: John Lawniczak
**************************************************/
BOOL printTens(int tens){
	//
	//print out the tens, mark the flag teen if we need to print out a teen number
	//
	BOOL teen = FALSE;
	switch (tens) {
	case 1:
		teen = TRUE;
		break;
	case 2:
		printf(" twenty ");
		break;
	case 3:
		printf(" thirty ");
		break;
	case 4:
		printf(" forty ");
		break;
	case 5:
		printf(" fifty ");
		break;
	case 6:
		printf(" sixty ");
		break;
	case 7:
		printf(" seventy ");
		break;
	case 8:
		printf(" eighty ");
		break;
	case 9:
		printf(" ninety ");
		break;
	}
	return teen;
}

/**************************************************
function Name: main
Input to the method: int argc : number of arguments, char** argv array of inputs
Output(Return value): int the exit status 1 if error, 0 success
Brief description of the task: takes console input of numbers and converts them to english words representing them
Author: John Lawniczak
**************************************************/

int main(int argc, char ** argv) {
	long rawNumber = 0;
	int sections[3];
	int scanResult = 0;

	//
	//scan the input from the console; scanResult will be 1 if its good and 0 if it fails to match the input to a decimal
	//

	printf("Please feed a number :");
	scanResult = scanf("%ld", &rawNumber);

	while(rawNumber != 0){

		//
		//return if the number is not between 0 and 1000000000 and if the scan result is valid
		//

		if ((rawNumber > 1000000000 || rawNumber < 0) || scanResult == 0) {
			printf("enter a number between 0 and 999999999");
		}else{

			printf("you entered this number :");

			//
			//parse the large number into an array containing number for the hundres, thousands and millions
			// sending a copy of the raw number
			//

			int copyRawNum = rawNumber;
			parselargeNumber(sections, copyRawNum);


			//
			//go through each section(hundres,thousands,millions) and print out the number it represents
			//

			for (int i = 0; i < 3 && rawNumber > 0; i++) {
				int hundred = sections[i] / (100);
				int tens = (sections[i] / 10) % 10;
				int units = (sections[i] % 10);
				BOOL teen = FALSE;

				//
				//print out the hundreds part of the section
				//
				printDigit(hundred);
				if(hundred!=0)
					printf(" hundred ");

				//
				//print out the tens, if we need to print out the funky english teens then remember that
				//

				teen = printTens(tens);

				//
				//print out the units/ones and if we are printing a teen number print out the correct english form
				//

				if(!teen){
					printDigit(units);
				}else{
					printTeen(units);
				}

				//
				//if all the stuff we printed out is the millions or thousands places then print out it accordingly
				//

				if (sections[i] != 0) {
					if (i == 0)
						printf(" million ");
					else if (i == 1)
						printf(" thousand ");
				}
			}
		}

		//
		//scan the input from the console; scanResult will be 1 if its good and 0 if it fails to match the input to a decimal
		//

		printf("\nPlease feed a number :");
		scanResult = scanf("%ld", &rawNumber);

	}

	printf(" Thankyou for using the program ");

	return 0;
}

